package com.itboye.mengwu.app.ui.aizhiwu;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.BasePtr;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.adapter.MengTimesAdapter;
import com.itboye.mengwu.data.model.LuntanTieZiBean;
import com.itboye.mengwu.data.model.TieZiBaoHanReply;
import com.itboye.mengwu.data.model.TieZiBaoHanReply.GetAList;
import com.itboye.mengwu.data.presenter.MyTieZiCenterPresenter;
import com.itboye.mengwu.data.presenter.TieZiPresenter;
import com.itboye.mengwu.widget.XListView.IXListViewListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

public class AizhiwuMengShiGuangItemFragment extends BaseActivity implements
        OnClickListener, Observer, IXListViewListener {

    // CarouselView topLunbo;
    private List<ImageView> bmps;
    private ListView listView;
    TieZiPresenter presenter;
    // private View headerView;
    MengTimesAdapter adapter;
    ImageView back;
    TextView txt_title;
    LuntanTieZiBean model = new LuntanTieZiBean();
    MyTieZiCenterPresenter presenter2;
    private TieZiBaoHanReply baoHanReply;
    String uid;
    String type = "load";
    private int pageNo = 1;
    ArrayList<GetAList> aLists = new ArrayList<>();
    RelativeLayout nodata;
    TextView nodata_tv;
    LinearLayout head_lin;
    PtrFrameLayout ptr;
    @Override
    public int initView() {
        // TODO Auto-generated method stub
        return R.layout.activity_mengshiguang;
    }

    @Override
    public void onMyCreate() {
        showProgressDialog("加载中，请稍后", true);
        txt_title.setText("时光机");
        BasePtr.setPagedPtrStyle(ptr);
        presenter2 = new MyTieZiCenterPresenter(this);

        ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                pageNo++;
                uid = SPUtils.get(AizhiwuMengShiGuangItemFragment.this, null, SpPublicParams.UID, "") + "";
                model.setFid("9");
                model.setUid(uid);
                model.setPage_no(pageNo + "");
                presenter2.getInfo(model);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                pageNo = 1;
                uid = SPUtils.get(AizhiwuMengShiGuangItemFragment.this, null, SpPublicParams.UID, "") + "";
                model.setFid("9");
                model.setUid(uid);
                model.setPage_no(pageNo + "");
                presenter2.getInfo(model);
            }
        });
        head_lin.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(
                        AizhiwuMengShiGuangItemFragment.this,
                        MengShiGuangLook.class);
                intent.putExtra("tid", "-1");
                startActivityForResult(intent, 101);
                // (intent);
            }
        });
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent intent = new Intent(
                        AizhiwuMengShiGuangItemFragment.this,
                        MengShiGuangLook.class);
                intent.putExtra("tid", aLists.get(position ).getTid());
                startActivity(intent);
            }
        });

    }

    /**
     * 设置数据
     */
    private void setData() {
        adapter = new MengTimesAdapter(AizhiwuMengShiGuangItemFragment.this,
                aLists, editOnclickListener);
        // listView.addHeaderView(headerView);
        listView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        uid = SPUtils.get(this, null, SpPublicParams.UID, "") + "";
        presenter = new TieZiPresenter(this);
        model.setFid("9");
        model.setUid(uid);
        model.setPage_no(pageNo + "");
        presenter2.getInfo(model);
        setData();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;

            default:
                break;
        }
    }

    OnClickListener editOnclickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int position = (int) v.getTag();
            Intent intent = new Intent(AizhiwuMengShiGuangItemFragment.this,
                    MengShiGuangLook.class);
            intent.putExtra("tid", aLists.get(position).getTid());
            intent.putExtra("edit", "edit");
            startActivityForResult(intent, 101);
        }
    };

    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        super.onActivityResult(arg0, arg1, arg2);
        if (arg0 == 101 && arg1 == 102) {
            model.setFid("9");
            model.setUid(uid);
            model.setPage_no(pageNo + "");
            // presenter2.getInfo(model);
            presenter.getInfo(model);
        }
    }

    ;

    @Override
    public void update(Observable observable, Object data) {
        // TODO Auto-generated method stub
        ptr.refreshComplete();
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == TieZiPresenter.TieziFenYeSuccess) {
            } else if (entity.getEventType() == MyTieZiCenterPresenter.QueryMyTieZiSuccess) {
                baoHanReply = (TieZiBaoHanReply) entity.getData();
                if (pageNo == 1) {
                    aLists.clear();
                } else {

                }
                aLists.addAll(baoHanReply.getList());
                if (aLists.size() == 0) {
                    // listView.removeAllViews();
                    nodata.setVisibility(View.VISIBLE);
                    // listView.addHeaderView(headerView);
                    listView.setVisibility(View.GONE);
                    nodata_tv.setText("暂无数据哦，赶快去发布吧");
                } else {
                    nodata.setVisibility(View.GONE);
                    listView.setVisibility(View.VISIBLE);
                }

                adapter.notifyDataSetChanged();
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    @Override
    public void onRefresh() {
        // TODO Auto-generated method stub
        showProgressDialog("刷新中。。。", true);
        pageNo = 1;
        type = "refresh";
        model.setFid("9");
        model.setUid(uid);
        model.setPage_no(pageNo + "");
        // presenter2.getInfo(model);
        presenter.getInfo(model);
    }

    @Override
    public void onLoadMore() {
        // TODO Auto-generated method stub
        showProgressDialog("刷新中。。。", true);
        pageNo++;
        type = "load";
        model.setFid("9");
        model.setUid(uid);
        model.setPage_no(pageNo + "");
        // presenter2.getInfo(model);
        presenter.getInfo(model);
    }

}
